package com.frxuexi.mdm_2207.myview

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.RelativeLayout
import com.frxuexi.lib_base.BaseApp
import com.frxuexi.lib_base.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.Random
import java.util.Timer
import kotlin.concurrent.timerTask

class MyHBView:RelativeLayout {
    lateinit var timer: Timer
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    @SuppressLint("ObjectAnimatorBinding")
    fun start(){
        timer= Timer()
        timer.schedule(timerTask {
                GlobalScope.launch(Dispatchers.Main) {
                    val imageView = ImageView(BaseApp.context)
                    imageView.setImageResource(R.drawable.hb)

                    val num = Random().nextInt(1000)

                    val params = LayoutParams(100, 200)
                    params.topMargin=0
                    params.leftMargin=num
                    imageView.layoutParams=params
                    addView(imageView)
                    val animator = ObjectAnimator.ofFloat(imageView,"translationY",0f, 2000f)
                    animator.duration=3000
                    animator.start()

                }
        },0,300)
    }

}